package com.ZeroCarbon.service.impl;

import com.ZeroCarbon.client.UserClient;
import com.ZeroCarbon.domain.po.Address;
import com.ZeroCarbon.exception.BusinessLogicException;
import com.ZeroCarbon.mapper.AddressMapper;
import com.ZeroCarbon.method.RemoteClientCall;
import com.ZeroCarbon.service.IAddressService;
import com.ZeroCarbon.utils.UserHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

import static com.ZeroCarbon.exception.BusinessLogicException.ERROR_EXCEPTION;
import static com.ZeroCarbon.method.RemoteClientCall.OperationType.GET_BASE_INFO;
import static com.ZeroCarbon.prefix.MySqlKey.ADDRESS_PREFIX;
import static com.ZeroCarbon.prefix.MySqlKey.generateId;

@Service
@RequiredArgsConstructor
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    /**
     * 获取所有地址
     */
    @Override
    public List<Address> getAllAddress() throws BusinessLogicException {
        //获取用户id
        String userId = UserHolder.getUser();
        //获取地址构造器
        LambdaQueryWrapper<Address> getAddressWrapper = new LambdaQueryWrapper<Address>()
                .eq(Address::getUserId, userId);
        //记录地址列表
        List<Address> addressList;
        try {
            //获取地址结果
            addressList = list(getAddressWrapper);
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
        //没有获取到结果
        if (addressList == null || addressList.isEmpty()) {
            return Collections.emptyList();
        }
        return addressList;
    }

    /**
     * 添加地址
     *
     * @param address 地址详细信息
     */
    @Override
    public void addNewAddress(Address address) throws BusinessLogicException {
        //获取地址构造器
        LambdaQueryWrapper<Address> getAddressWrapper = new LambdaQueryWrapper<Address>()
                .eq(Address::getUserId, address.getUserId());
        //最多添加三个收货地址
        if (count(getAddressWrapper) >= 3) {
            throw new BusinessLogicException("添加的收获地址不能超过3个");
        }
        try {
            //设置新的地址id
            String id = generateId(ADDRESS_PREFIX);
            //保存结果
            if (!save(address.setId(id))) {
                throw new BusinessLogicException();
            }
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }

    /**
     * 获取当前账号地址
     */
    @Override
    public Address getCurrentAddress() throws BusinessLogicException {
        //获取用户id
        String userId = UserHolder.getUser();
        //生成地址id
        String id = generateId(ADDRESS_PREFIX);
        //记录地址
        Address address;
        try {
            //远程调用获取用户信息
            address = RemoteClientCall.getClientResult(
                    new Class[]{UserClient.class},
                    GET_BASE_INFO,
                    Address.class,
                    new Object[]{userId});
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
        //设置信息返回结果
        return address.setUserId(address.getId())
                .setId(id);
    }

    /**
     * 更新地址
     *
     * @param address 地址具体信息
     */
    @Override
    public void updateAddress(Address address) throws BusinessLogicException {
        //地址构造器
        LambdaQueryWrapper<Address> addressWrapper = new LambdaQueryWrapper<Address>()
                .eq(Address::getId, address.getId());
        try {
            //更新结果
            if (!update(address, addressWrapper)) {
                throw new BusinessLogicException();
            }
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }
}
